﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common.Communication
{
    public class HttpClientBase
    {
        static HttpClient client = new HttpClient();
        /// <summary>
        /// post请求
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <param name="json">参数</param>
        /// <returns></returns>
        public static string Post(string url, string json)
        {
            try
            {
                //todo  jobject
                ServicePointManager.Expect100Continue = false;

                var content = new StringContent(json, Encoding.UTF8, "application/json");
                var response = client.PostAsync(url, content).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        /// <summary>
        /// get请求
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <param name="value">参数</param>
        /// <returns></returns>
        public static string Get(string url, string value)
        {
            try
            {
                ServicePointManager.Expect100Continue = false;

                var content = client.GetStringAsync(url + value).Result;

                return content;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
